package com.example.shiyan7.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.shiyan7.R;

public class musichelper2 extends Fragment {
    public musichelper2(){super(R.layout.musichelper2);}
    MyConn myConn = null;

    public class MyConn implements ServiceConnection {
        public MusicService2.MyBinder myBinder = null;

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MusicService2.MyBinder)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Intent intent = new Intent(getActivity(),MusicService2.class);
        getActivity().startService(intent);

        //bindService
        if(myConn == null)
        {
            myConn = new MyConn();
            intent = new Intent(getActivity(),MusicService2.class);
            getActivity().bindService(intent,myConn,0);
        }

        //播放按钮
        Button button9 = getView().findViewById(R.id.button9);
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myConn.myBinder == null){
                    return;
                }
                myConn.myBinder.play();
            }
        });

        //暂停按钮
        Button button11 = getView().findViewById(R.id.button11);
        button11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myConn.myBinder == null){
                    return;
                }
                myConn.myBinder.pause();
            }
        });

    }
}
